import pandas as pd
import numpy as np


def op_basic_test():
    """基本测试"""
    # create with DatetimeIndex
    dates = pd.date_range('20130101', periods=6)
    data = np.arange(0, 24, 1).reshape(6, 4)
    # data = np.random.randn(6,4)
    df = pd.DataFrame(data, index=dates, columns=list('ABCD'))
    print("df:\n", df)
    # axis=0
    print("df.mean():\n", df.mean())
    # axis = 1
    print("df.mean(1):\n", df.mean(1))

    # Operating with objects that have different dimensionality and need alignment.
    # In addition, pandas automatically broadcasts along the specified dimension.
    s = pd.Series([1,3,5,np.nan,6,8], index=dates)
    print("series s:\n", s)
    print("series s.shift(2):\n", s.shift(-2))
    df_sub = df.sub(s, axis='index')
    print("df_sub:\n", df_sub)

    df_sum = df.apply(np.cumsum)
    print("df_cumsum:\n", df_sum)
    df_maxdiff = df.apply(lambda x: x.max() - x.min(), axis=0)
    print("df_maxdiff:\n",df_maxdiff)

    # Histogramming
    print("--Histogramming--")
    s = pd.Series(np.random.randint(0, 7, size=10))
    s_counts = s.value_counts() # 各个值出现的次数
    print("s_count:\n", s_counts)

    # String Methods
    print("--String Methods--")
    s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
    print(s.str.lower())

if __name__ == '__main__':
    op_basic_test()




